package com.baidu.littlepainter.fragments.sheet

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.baidu.littlepainter.databinding.LayoutUserItemBinding
import com.baidu.littlepainter.db.User

/**
 * 离开界面 ->
 * 从界面删除 ->
 */
class UserAdapter: RecyclerView.Adapter<UserAdapter.MyViewHolder>() {
    private var users:List<User> = emptyList()
    private var mCallBack:(User)->Unit = {}

    inner class MyViewHolder(val binding:LayoutUserItemBinding): RecyclerView.ViewHolder(binding.root){
        fun bind(user: User){
            binding.tvName.text = user.name
            binding.root.setOnClickListener {
                //获取到用户
                mCallBack(user)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding = LayoutUserItemBinding.inflate(layoutInflater, parent, false)
        return MyViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return users.size
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        if(users.isEmpty()) return

        holder.bind(users[position])
    }

    fun setUsers(users: List<User>){
        this.users = users
        notifyDataSetChanged()
    }

    //提供给外部接收选中的回调事件
    fun addSelectCallback(action:(User)->Unit){
        this.mCallBack = action
    }
}